import React from 'react';
import { Icon , Button , Modal , Tooltip , Spin } from 'antd';
import {WeaCheckbox , WeaTop, WeaPopoverHrm, WeaLocaleProvider} from 'ecCom';
import * as Util from "../../../util/Util";
const getLabel = WeaLocaleProvider.getLabel;
export default class ButtonTop extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            btns:[],
        }
    }

    getBtns = (detailKey,data) =>{
        let btns = [];
        if(data && data.length > 0){
            btns = data
        }else{
            let btns = this.state.btns;
        }
        
        let buttons = [];
        for(let i = 0;i < btns.length; i ++){
            let current = btns[i]
            if(!Util.isEmpty(current.tip)){
                 let btn =  <Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@p4wx0f`} title = {current.tip[current.valid]}><Button ecId={`${this && this.props && this.props.ecId || ''}_Button@egct8y`} id={i} type={current.type[current.valid]} ref="myButton" onClick={() =>this.click(i,current.key,detailKey) }>{current.value[current.valid]}</Button></Tooltip>;
                 buttons.push(btn)
            }else{
                 let btn =  <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@jeiu9v`} id={i} type={current.type[current.valid]} ref="myButton" onClick={() =>this.click(i,current.key,detailKey) }>{current.value[current.valid]}</Button>;
                 buttons.push(btn)
            }
        }
        return buttons;
    }

    click = (id,type,detailKey) => {
        let {buttonDatas} = this.props;
        let buttons = [];
        if(buttonDatas && buttonDatas.length > 0){
            buttons = buttonDatas;
        }else{
            buttons = this.state.btns;
        }
        if(buttons[id].key == type){
            if(buttons[id].value.length == 2){
                if(buttons[id].valid == 0){
                    buttons[id].valid = 1;
                }else{
                    buttons[id].valid = 0;
                }
            }
            this.props.method(detailKey,type,buttons[id].valid)  
        }
        
        this.setState({
                    btns:buttons,
                })
    }


    onlyClose = () =>{
        if(typeof this.props.onClose === 'function'){
            this.props.onClose({type:1})
        }
    }

    render(){
        const {openType,detailkey,buttonDatas,detailData,iconType} = this.props;
        if(openType == "2" || openType == undefined){
                return (
                    <div className='wea-workrelate-buttonTop'>
                        <WeaPopoverHrm ecId={`${this && this.props && this.props.ecId || ''}_WeaPopoverHrm@w9z257`}/>
                        {<WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@8xicqo`}
                            title={Util.isEmpty(detailData.createrlink)?<span><span className="wea-workrelate-buttonTop-title" title={detailData.name?getLabel(18238,'目标名称')+":"+detailData.name:getLabel(507890,'目标详情')}>{detailData.name?detailData.name:getLabel(507890,'目标详情')}</span></span>:
                            <span><span className="wea-workrelate-buttonTop-title" title={detailData.name?getLabel(18238,'目标名称')+":"+detailData.name:getLabel(507890,'目标详情')}>{detailData.name?detailData.name:getLabel(507890,'目标详情')}</span>{<Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@hfvast`} title={getLabel(882,'创建人')+`:${detailData.username} `+ getLabel(30436,'创建时间')+`:${detailData.createdate+' '+detailData.createtime}`}><span className="wea-workrelate-goal-createinfo" dangerouslySetInnerHTML={{ __html:detailData.createrlink+detailData.createdate+' '+detailData.createtime}}></span></Tooltip>}</span>}
                            icon={iconType == '1'?<Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@tjwb8m`} title={getLabel(508788,'关闭侧边框')}><i className='icon-coms-Right icursor-pointer'  onClick={()=>this.onlyClose()}/></Tooltip>:<div className="wea-workrelate-buttonTop-goalDetail"><Tooltip ecId={`${this && this.props && this.props.ecId || ''}_Tooltip@07u3q0`}  title={getLabel(507890,'目标详情')}><i className='icon-coms-implement'/></Tooltip></div>}
                            buttons={this.getBtns(detailkey,buttonDatas)}
                            iconBgcolor={iconType == '1'?'':'rgb(150, 53, 138)'}
                        />}
                    </div>           
            )    
        }else{
            return <div></div>
        }             
    }


}